﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.Web.Mvc;
using System.Web.Routing;
using System.Security.Policy;

namespace Wild2.Models.User
{
    public class Settings
    {
        public static Dictionary<string,string> GetUserSettings(int userID)
        {
            Dictionary<string, string> settings = new Dictionary<string, string>();
            Object s = HttpContext.Current.Session[Wild2.Models.Constants.SessionConstants.SETTINGS];
            if (s == null)
            {
                String props = "";
                using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnect"].ConnectionString))
                {
                    SqlCommand cmd = new SqlCommand(@"SELECT [Settings] FROM [Users] WHERE ([ID] = @UID)", conn);
                    cmd.Parameters.Add("@UID", SqlDbType.Int);
                    cmd.Parameters["@UID"].Value = userID;
                    cmd.Connection.Open();
                    SqlDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        props = reader["Settings"].ToString();
                    }
                    reader.Close();
                    cmd.Connection.Close();
                }
                String[] propsArray = props.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (var item in propsArray)
                {
                    settings.Add(item.Split(new char[] { '=' })[0], item.Split(new char[] { '=' })[1]);
                }
            }
            else
            {
                settings = (Dictionary<string, string>)s;
            }
            return settings;
        }

        public static Dictionary<string, string> AddUserSettings(int userID, string key, string value)
        {
            Dictionary<string, string> settings = new Dictionary<string, string>();
            String props = "";
            using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnect"].ConnectionString))
            {
                SqlCommand cmd = new SqlCommand(@"SELECT [Settings] FROM [Users] WHERE ([ID] = @UID)", conn);
                cmd.Parameters.Add("@UID", SqlDbType.Int);
                cmd.Parameters["@UID"].Value = userID;
                cmd.Connection.Open();
                SqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    props = reader["Settings"].ToString();
                }
                reader.Close();
                cmd.Connection.Close();
            }
            if (String.IsNullOrEmpty(props)) props += key + "=" + value;
            else props += ";" + key + "=" + value;
            using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnect"].ConnectionString))
            {
                SqlCommand cmd = new SqlCommand(@"UPDATE [Users] SET [Settings] = @Props FROM [Users] WHERE ([ID] = @UID)", conn);
                cmd.Parameters.Add("@UID", SqlDbType.Int);
                cmd.Parameters.Add("@Props", SqlDbType.NVarChar, 4000);
                cmd.Parameters["@UID"].Value = userID;
                cmd.Parameters["@Props"].Value = props;
                cmd.Connection.Open();
                cmd.ExecuteNonQuery();
                cmd.Connection.Close();
            }
            String[] propsArray = props.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (var item in propsArray)
            {
                settings.Add(item.Split(new char[] { '=' })[0], item.Split(new char[] { '=' })[1]);
            }
            HttpContext.Current.Session[Wild2.Models.Constants.SessionConstants.SETTINGS] = settings;
            return settings;
        }

        public static Dictionary<string, string> DeleteUserSettings(int userID, string key)
        {
            Dictionary<string, string> settings = new Dictionary<string, string>();
            String props = "";
            using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnect"].ConnectionString))
            {
                SqlCommand cmd = new SqlCommand(@"SELECT [Settings] FROM [Users] WHERE ([ID] = @UID)", conn);
                cmd.Parameters.Add("@UID", SqlDbType.Int);
                cmd.Parameters["@UID"].Value = userID;
                cmd.Connection.Open();
                SqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    props = reader["Settings"].ToString();
                }
                reader.Close();
                cmd.Connection.Close();
            }
            String[] propsArray = props.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (var item in propsArray)
            {
                if (item.Split(new char[] { '=' })[0] != key) settings.Add(item.Split(new char[] { '=' })[0], item.Split(new char[] { '=' })[1]);
            }
            props = "";
            foreach (var item in settings)
            {
                if (String.IsNullOrEmpty(props)) props += item.Key + "=" + item.Value;
                else props += ";" + item.Key + "=" + item.Value;
            }
            using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnect"].ConnectionString))
            {
                SqlCommand cmd = new SqlCommand(@"UPDATE [Users] SET [Settings] = @Props FROM [Users] WHERE ([ID] = @UID)", conn);
                cmd.Parameters.Add("@UID", SqlDbType.Int);
                cmd.Parameters.Add("@Props", SqlDbType.NVarChar, 4000);
                cmd.Parameters["@UID"].Value = userID;
                cmd.Parameters["@Props"].Value = props;
                cmd.Connection.Open();
                cmd.ExecuteNonQuery();
                cmd.Connection.Close();
            }
            HttpContext.Current.Session[Wild2.Models.Constants.SessionConstants.SETTINGS] = settings;
            return settings;
        }

        public static string GetUserHomeUrl(int userID)
        {
            Dictionary<string, string> settings = GetUserSettings(userID);
            if (settings.Keys.Contains("homeurl"))
            {
                if (settings["homeurl"] == "my")
                {
                    return "Blog:My";
                }
                if (settings["homeurl"] == "all")
                {
                    return "Blog:All";
                }
                if (settings["homeurl"] == "top")
                {
                    return "Home:Index";
                }
                if (settings["homeurl"] == "sandbox")
                {
                    return "Blog:Sandbox";
                }
            }
            return "";
        }

        public static void SetUserHomeUrl(int userID, string name)
        {
            Dictionary<string, string> settings = GetUserSettings(userID);
            if (settings.Keys.Contains("homeurl")) DeleteUserSettings(userID, "homeurl");
            if (name == "my" || name == "all" || name == "top" || name == "sandbox")
            {
                AddUserSettings(userID, "homeurl", name);
            }
            if (String.IsNullOrEmpty(name))
            {
                DeleteUserSettings(userID, "homeurl");
            }
        }
    }
}